package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderLineAtomUpdateRequest;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName SalesOrderAtomUpdateRequestToAtomOrderDOConvertor
 * @description: TODO
 * @date 2023/04/25 20:16
 * @version: 1.0
 */

public class SalesOrderAtomUpdateRequestToAtomOrderDOConvertor {

    public static List<AtomOrderDO> convert(List<OrderAtomUpdateRequest> requestList) {
        return Optional.ofNullable(requestList).orElse(Lists.newArrayList()).stream().map(request -> {
            AtomOrderDO atomOrderDO = new AtomOrderDO();
            atomOrderDO.setOrder(convert(request));

            atomOrderDO.setOrderLines(convertOrderLine(request.getOrderDetails()));
            for (OrderLine orderLine : atomOrderDO.getOrderLines()) {
                orderLine.setModifierName(atomOrderDO.getOrder().getModifierName());
                orderLine.setModifierId(atomOrderDO.getOrder().getModifier());
            }
            return atomOrderDO;
        }).collect(Collectors.toList());
    }

    public static Order convert(OrderAtomUpdateRequest request) {
        Order order = new Order();
        order.setOrderBizId(request.getOrderReasonCode());
        if (StringUtils.isNotBlank(request.getExpectedDeliveryDate())) {
            order.setCustomerExpectDate(DateUtils.string2Date(request.getExpectedDeliveryDate()));
        }
        if (StringUtils.isNotBlank(request.getDeliveryMethodCode())) {
            order.setTransportModeCode(request.getDeliveryMethodCode());
            order.setTransportModeName(request.getDeliveryMethodName());
        }
        if (StringUtils.isNotBlank(request.getOrderReasonCode())) {
            order.setCreateReason(request.getOrderReasonCode());
            order.setCreateReasonName(request.getOrderReasonName());
        }
        if (StringUtils.isNotBlank(request.getNote())) {
            order.setRemark(request.getNote());
        }
        order.setModifier(request.getModifierCode());
        order.setModifierName(request.getModifierName());
        return order;
    }

    /**
     * 明细单行号 布进器
     **/
    private static final Integer Order_Line_Step = 10;

    public static List<OrderLine> convertOrderLine(List<OrderLineAtomUpdateRequest> orderDetails) {
        return Optional.ofNullable(orderDetails).orElse(Lists.newArrayList()).stream().map(lineRequest -> {
            // 循环行数 递增
            OrderLine lineSDO = new OrderLine();
            lineSDO.setSerialNumber(lineRequest.getLineNum());
            if (StringUtils.isNotBlank(lineRequest.getCloseCode())) {
                lineSDO.setRefuseReasonCode(lineRequest.getCloseCode());
                lineSDO.setRefuseReasonName(lineRequest.getRefuseReasonName());
            }
            return lineSDO;
        }).collect(Collectors.toList());
    }

}
